package com.shop.mms_client;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.shop.mms_client.service.SmsListenerService;
import com.shop.mms_client.ui.MessagesPagerAdapter;
import com.shop.mms_client.utils.PermissionManager;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initViews();
        checkPermissions();
        
        // 添加测试按钮
        findViewById(R.id.testButton).setOnClickListener(v -> {
            startActivity(new Intent(this, TestActivity.class));
        });
    }

    private void initViews() {
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        
        viewPager.setAdapter(new MessagesPagerAdapter(this));
        
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("所有消息");
                    break;
                case 1:
                    tab.setText("垃圾消息");
                    break;
                case 2:
                    tab.setText("黑名单");
                    break;
            }
        }).attach();
    }

    private void checkPermissions() {
        PermissionManager.checkAndRequestPermissions(this, 
            new PermissionManager.PermissionCallback() {
                @Override
                public void onGranted() {
                    startSmsListenerService();
                }

                @Override
                public void onDenied(List<String> deniedPermissions) {
                    showPermissionError(deniedPermissions);
                }
            });
    }

    private void startSmsListenerService() {
        Intent serviceIntent = new Intent(this, SmsListenerService.class);
        startService(serviceIntent);
        showServiceStartedMessage();
    }

    private void showPermissionError(List<String> deniedPermissions) {
        StringBuilder message = new StringBuilder("需要以下权限才能运行应用:\n");
        for (String permission : deniedPermissions) {
            message.append("- ").append(getPermissionName(permission)).append("\n");
        }
        
        Snackbar.make(viewPager, message.toString(), Snackbar.LENGTH_INDEFINITE)
            .setAction("重试", v -> checkPermissions())
            .show();
    }

    private void showServiceStartedMessage() {
        Toast.makeText(this, "短信监控服务已启动", Toast.LENGTH_SHORT).show();
    }

    private String getPermissionName(String permission) {
        switch (permission) {
            case android.Manifest.permission.RECEIVE_SMS:
                return "接收短信";
            case android.Manifest.permission.READ_SMS:
                return "读取短信";
            default:
                return permission;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止服务
        stopService(new Intent(this, SmsListenerService.class));
    }
}